CMAKE_MINIMUM_REQUIRED(VERSION 2.8.6)

#### AddTest

MACRO(AddCoverage COVERAGE_SCOPE TST_NAME )
    SET ( COVERAGE_SCOPE_NAME "COVERAGE_${COVERAGE_SCOPE}" )
    STRING ( TOLOWER ${TST_NAME} COVERAGE_NAME )
    SET ( ${COVERAGE_SCOPE_NAME} "${${COVERAGE_SCOPE_NAME}}" "${COVERAGE_NAME}" CACHE INTERNAL "" )
ENDMACRO(AddCoverage)

# Instructions for adding a simple test
MACRO(SimpleTest TestDirectory TestExeName TST_SOURCES)
    STRING ( TOUPPER ${TestDirectory} TestName )

    ADD_EXECUTABLE ( ${TestExeName} ${TST_SOURCES} )
    IF ( CMAKE_SIZEOF_VOID_P EQUAL 8 )
        GET_TARGET_PROPERTY ( TGT_LNK_FLG ${TestExeName} LINK_FLAGS )
        GET_TARGET_PROPERTY(TARGET_COMPILE_FLAGS ${TestExeName} COMPILE_FLAGS )
        IF ( ${TARGET_COMPILE_FLAGS} STREQUAL "TARGET_COMPILE_FLAGS-NOTFOUND" )
            SET ( TARGET_COMPILE_FLAGS "" )
        ENDIF ( ${TARGET_COMPILE_FLAGS} STREQUAL "TARGET_COMPILE_FLAGS-NOTFOUND" )
        SET ( TARGET_COMPILE_FLAGS "${TARGET_COMPILE_FLAGS} -m32 " )

        GET_TARGET_PROPERTY(TARGET_LINK_FLAGS ${TestExeName} LINK_FLAGS )
        IF ( "${TARGET_LINK_FLAGS}" STREQUAL "TARGET_LINK_FLAGS-NOTFOUND" )
            SET ( TARGET_LINK_FLAGS "" )
        ENDIF ( "${TARGET_LINK_FLAGS}" STREQUAL "TARGET_LINK_FLAGS-NOTFOUND" )
        SET ( TARGET_LINK_FLAGS "${TARGET_LINK_FLAGS} -m32 " )

            SET_TARGET_PROPERTIES(${TestExeName} PROPERTIES COMPILE_FLAGS ${TARGET_COMPILE_FLAGS}
                                                            LINK_FLAGS ${TARGET_LINK_FLAGS} )
    ENDIF()

    IF ( ( WIN32 OR CYGWIN ) AND ( ${LIB_TYPE} STREQUAL "SHARED" ) )
        EnsureLibraries(${TestExeName} "CUnit" )
    ENDIF ( ( WIN32 OR CYGWIN ) AND ( ${LIB_TYPE} STREQUAL "SHARED" ) )

    TARGET_LINK_LIBRARIES ( ${TestExeName} CUnit )

    ADD_TEST( ${TestName} ${PROJECT_BINARY_DIR}/${TestExeName} )

ENDMACRO(SimpleTest)

MACRO(AddDividedCoverage COVERAGE_SCOPE TestDirectory TST_NAME )
    SET ( COVERAGE_SCOPE_NAME "COVERAGE_${COVERAGE_SCOPE}" )
    STRING ( TOLOWER ${TestDirectory} TestSuiteName )

    IF ( UNITTEST_SMALL_TARGETS )
        FOREACH ( TST_COMMAND_NAME IN ITEMS ${TST_NAME} )
            STRING ( TOLOWER "${TestDirectory}_${TST_COMMAND_NAME}" COVERAGE_NAME )
            SET ( ${COVERAGE_SCOPE_NAME} "${${COVERAGE_SCOPE_NAME}}" "${COVERAGE_NAME}" CACHE INTERNAL "" )
        ENDFOREACH ( TST_COMMAND_NAME in ITEMS ${TST_NAME} )
    ELSE ( UNITTEST_SMALL_TARGETS )
        STRING ( TOLOWER ${TestSuiteName} COVERAGE_NAME )
        SET ( ${COVERAGE_SCOPE_NAME} "${${COVERAGE_SCOPE_NAME}}" "${COVERAGE_NAME}" CACHE INTERNAL "" )
    ENDIF ( UNITTEST_SMALL_TARGETS )

ENDMACRO(AddDividedCoverage)

MACRO(DividedTest TestDirectory SUB_TESTS_COMMANDS TST_SOURCES )
    STRING ( TOUPPER ${TestDirectory} TestSuiteName )

    IF ( UNITTEST_SMALL_TARGETS )
        SET ( TST_RUN "0" )
        FOREACH ( TST_COMMAND_NAME IN ITEMS ${SUB_TESTS_COMMANDS} )

            STRING ( TOLOWER ${TST_COMMAND_NAME} TST_EXE_NAME )

            ADD_EXECUTABLE ( ${TST_EXE_NAME} ${TST_SOURCES} )

            IF ( ( WIN32 OR CYGWIN ) AND ( ${LIB_TYPE} STREQUAL "SHARED" ) )
              EnsureLibraries(${TST_EXE_NAME} "CUnit" )
            ENDIF ( ( WIN32 OR CYGWIN ) AND ( ${LIB_TYPE} STREQUAL "SHARED" ) )

            GET_PROPERTY ( COMP_FLAGS TARGET ${TST_EXE_NAME} PROPERTY COMPILE_FLAGS)
            SET(COMP_FLAGS "${COMP_FLAGS} -D${TST_COMMAND_NAME} ")
            SET_TARGET_PROPERTIES (${TST_EXE_NAME} PROPERTIES COMPILE_FLAGS ${COMP_FLAGS})

            TARGET_LINK_LIBRARIES ( ${TST_EXE_NAME} CUnit )
            ADD_TEST( ${TST_COMMAND_NAME} ${PROJECT_BINARY_DIR}/${TST_EXE_NAME} )

            SET ( TST_RUN "1" )

        ENDFOREACH ( TST_COMMAND_NAME IN ITEMS ${SUB_TESTS_COMMANDS} )

        IF ( ${TST_RUN} STREQUAL "0" )
            MESSAGE(FATAL_ERROR "You have to supply a test scenario for ${TestSuiteName}!" )
        ENDIF ( ${TST_RUN} STREQUAL "0" )

    ELSE( UNITTEST_SMALL_TARGETS )

        SET ( TST_COMPILE_FLAGS "" )
        FOREACH ( TST_COMMAND_NAME IN ITEMS ${SUB_TESTS_COMMANDS} )
            SET ( TST_COMPILE_FLAGS "${TST_COMPILE_FLAGS} -D${TST_COMMAND_NAME}" )
        ENDFOREACH ( TST_COMMAND_NAME IN ITEMS ${SUB_TESTS_COMMANDS} )

        STRING ( TOLOWER ${TestSuiteName} TST_EXE_NAME )

        ADD_EXECUTABLE ( ${TST_EXE_NAME} ${TST_SOURCES} )

        GET_PROPERTY ( COMP_FLAGS TARGET ${TST_EXE_NAME} PROPERTY COMPILE_FLAGS)
        SET(COMP_FLAGS "${COMP_FLAGS} ${TST_COMPILE_FLAGS} ")
        SET_TARGET_PROPERTIES (${TST_EXE_NAME} PROPERTIES COMPILE_FLAGS ${COMP_FLAGS})

        TARGET_LINK_LIBRARIES ( ${TST_EXE_NAME} CUnit )
        ADD_TEST( ${TestSuiteName} ${PROJECT_BINARY_DIR}/${TST_EXE_NAME} )

    ENDIF ( UNITTEST_SMALL_TARGETS )

ENDMACRO(DividedTest)
